/**
 * 04/12/2013
 * 
 * 
 * 
 * Copyright GPL
 */
package com.jtmproject.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.jtmproject.classes.HttpUtils;
import com.jtmproject.database.Role;
import com.jtmproject.database.RoleDAO;
import com.jtmproject.database.User;
import com.jtmproject.database.UserDAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * This is an action to show
 * @author Javier Tejedor
 */
public class Users extends ActionSupport {
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -5027611362562431121L;

	private List<User> listUsers = new ArrayList<User>();
	private List<Role> listRoles = new ArrayList<Role>();
	private String roleValue;
	private int id;
	
	
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		
		return SUCCESS;
	}
	
	/**
	 * this selects all the users
	 * @return
	 */
	public String selectAllUsers() {
		listUsers.clear();
		listUsers.addAll(UserDAO.getUserDAO().selectAll());
		
		chargeAllRoles();
		return SUCCESS;
	}
	
	/**
	 * this selects all roles
	 */
	public void chargeAllRoles() {
		listRoles.clear();
		listRoles.addAll(RoleDAO.getRoleDAO().selectAll());
	}
	
	/**
	 * This method deletes one row with the id passed by parameters.
	 * @return
	 */
	public String delete(){
		User user;
		
		String id = (String) HttpUtils.getGetParameters(
				ActionContext.getContext(), "id", true);
		
		if(id != null){
			
			user = new User();
			user.setId(Integer.parseInt(id));
			
			UserDAO.getUserDAO().delete(user);

		}
		
		return SUCCESS;
	}
	
	public String getUsersByRol(){
		String rol = roleValue;
		System.out.println(rol);
		System.out.println("teje" + id);
		
		listUsers.clear();
		listUsers.addAll(UserDAO.getUserDAO().selectAllByRol(getId(rol)));
		
		chargeAllRoles();
		return SUCCESS;
	}
	
	/**
	 * this returns the id
	 * @param rolText
	 * @return
	 */
	private int getId(String rolText){
		rolText = rolText.replaceAll(" ", "");
		String[] split = rolText.split("-");
		
		return Integer.parseInt(split[split.length - 1]);
	}
	
	/**
	 * this returns the list of the users
	 * @return
	 */
	public List<User> getListUsers() {
		return listUsers;
	}

	/**
	 * this sets the list of the users
	 * @param listUsers
	 */
	public void setListUsers(List<User> listUsers) {
		this.listUsers = listUsers;
	}
	
	/**
	 * returns the list of the roles
	 * @return
	 */
	public List<Role> getListRoles() {
		return listRoles;
	}

	/**
	 * sets the list of the roles
	 * @param listRoles
	 */
	public void setListRoles(List<Role> listRoles) {
		this.listRoles = listRoles;
	}

	/**
	 * returns the role's value
	 * @return
	 */
	public String getRoleValue() {
		return roleValue;
	}

	/**
	 * sets the role's value
	 * @param roleValue
	 */
	public void setRoleValue(String roleValue) {
		this.roleValue = roleValue;
	}

	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	/* (non-Javadoc)
     * @see com.opensymphony.xwork2.ActionSupport#input()
     */
    @Override
    public String input() throws Exception{
    	return super.input();
    }
    
}